1671C - Dolce Vita - CodeForces Solution


binary search greedy math

Please click on ads to support us..

Python Code:

from functools import lru_cache
def get_last_term(start,end,d):
	return ((end - start) // d) + 1

for _ in range(int(input())):
	shops, b = map(int,input().split())
	costs = list(map(int,input().split()))
	costs.sort()
	p_sum = []
	s = 0
	for c in costs:
		s += c
		p_sum.append(s)
	ans = 0
	for i in range(len(p_sum)):
		last_term = get_last_term(p_sum[i],b,i+1)
		if last_term >= 0:
			ans += last_term
	print(ans)

C++ Code:

#include<bits/stdc++.h>
#define ll long long
#define yes cout<<"YES\n";
#define no cout<<"NO\n";
#define debug(x) cout << "(" << #x << ": " << x << ")\n";

using namespace std;
int inf=-10000000;


ll n,x;
ll a[200200];
void solve(){
    cin>>n>>x;
    for(int i=0;i<n;i++)
        cin>>a[i];
    sort(a,a+n);
    for(int i=1;i<n;i++)
        a[i]+=a[i-1];
    ll ans = 0;
    for(int i=0;i<n;i++){
        if(x-a[i]>=0){
            ans+= ((x-a[i])/(i+1)) + 1;
        }
    }
    cout<<ans<<'\n';
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int T;
    cin>>T;
    while(T--)
        solve();
}


Comments

Submit
0 Comments
More Questions

1093A - Dice Rolling
1360B - Honest Coach
1399C - Boats Competition
1609C - Complex Market Analysis
1657E - Star MST
1143B - Nirvana
1285A - Mezo Playing Zoma
919B - Perfect Number
894A - QAQ
1551A - Polycarp and Coins
313A - Ilya and Bank Account
1469A - Regular Bracket Sequence
919C - Seat Arrangements
1634A - Reverse and Concatenate
1619C - Wrong Addition
1437A - Marketing Scheme
1473B - String LCM
1374A - Required Remainder
1265E - Beautiful Mirrors
1296A - Array with Odd Sum
1385A - Three Pairwise Maximums
911A - Nearest Minimums
102B - Sum of Digits
707A - Brain's Photos
1331B - Limericks
305B - Continued Fractions
1165B - Polycarp Training
1646C - Factorials and Powers of Two
596A - Wilbur and Swimming Pool
1462B - Last Year's Substring